(function () {
  /**
   * 页首脚本
   */
  var loadingProcess = 0;
  var isLoading = true;
  var $loadingProcess = $('#loadingProcess');

  // 改造评论结构与样式，展示用户头像
  function modifyComment() {
    $('.feedbackCon>.blog_comment_body').each(function (index, item) {
      var faceIcon = 'http://images.cnblogs.com/cnblogs_com/vvjiang/996881/o_default_face.jpg'
      var commentId = $(item).attr('id').split('_')[2];
      // 取消评论下所有的链接上的事件，@上的事件真是奇怪
      $(item).find('a').attr('onclick', '');
      // 为了点击@有效，得加上id为评论id的功能
      if ($('#' + commentId).length === 0) {
        $(item).parent().parent().attr('id', commentId);
      }
      // 取得博客园提供的隐藏头像信息
      var $hiddenInfo = $('#comment_' + commentId + '_avatar')
      if ($hiddenInfo.length > 0) {
        faceIcon = $hiddenInfo.text();
      }
      // 获取评论名字的元素
      var nameId = '#a_comment_author_' + commentId;
      var $name = $(nameId);

      // 得到头像元素
      var faceEle = '<a class="comment_face" href="' + $name.attr('href') + '" target="_blank" style="background-image: url(' + faceIcon + ')"></a>';

      $name.addClass('comment_name').after(faceEle);
    });
  }
  // 直到评论加载完毕才开始修改评论样式
  function beginModifyComment() {
    if ($('#comment_pager_bottom').length === 0) {
      setTimeout(beginModifyComment, 500)
    } else {
      modifyComment()
    }
  }
  // 生成文章的目录结构
  function createCatalog() {
    var $h2Arr = $('#cnblogs_post_body>h2');
    if ($h2Arr.length > 0) {
      var catalogContent = [].join.call($h2Arr.map(function (index, item) {
        return '<li><a id="catalog' + item.id + '" title="' + item.innerText + '" href="#' + item.id + '">' + item.innerText + '</a></li>'
      }), '');

      $('body').append('<div id="reader_catalog"><h3>目录</h3><ul>' + catalogContent + '</ul></div>')
    }
    createCatalogLeaderLine($h2Arr);
  }
  // 生成目录上的指示箭头
  function createCatalogLeaderLine($h2Arr) {
    var lines = {};
    var options = {
      color: '#5bf',
      endPlug: "disc",
      size: 2,
      startSocket: "left",
      endSocket: "right",
      hide: true
    };
    [].slice.call($h2Arr).forEach(function (item) {
      var anchor = LeaderLine.mouseHoverAnchor(document.getElementById('catalog' + item.id), 'draw', {
        animOptions: {
          duration: 500
        },
        hoverStyle: {
          backgroundColor: null
        },
        style: {
          paddingTop: null,
          paddingRight: null,
          paddingBottom: null,
          paddingLeft: null,
          cursor: null,
          backgroundColor: null,
          backgroundImage: null,
          backgroundSize: null,
          backgroundPosition: null,
          backgroundRepeat: null
        },
        onSwitch: function (event) {
          var line = lines[item.id]
          // 浮动上去就重绘
          if (event.type == "mouseenter") {
            line.position();
          }
        }
      });
      lines[item.id] = new LeaderLine(
        anchor,
        document.getElementById(item.id),
        options
      );
    })
    // 滚动时重绘指示箭头
    $(window).scroll(function () {
      for (var key in lines) {
        lines[key].position()
      }
    })
  }
  // 页面加载进度条
  function loading() {
    loadingProcess += 1;
    if (loadingProcess >= 80) {
      loadingProcess = 80;
    }
    $loadingProcess.css('width', loadingProcess + '%');
    if (!isLoading && loadingProcess === 80) {
      $loadingProcess.css('width', '100%').hide(200);
    } else {
      requestAnimationFrame(loading);
    }
  }
  // 图片放大功能
  function bindClickEvent4EnlargeImg() {
    $('#cnblogs_post_body').on('click', function (e) {
      if (e.target.tagName === 'IMG') {
        $(e.target).toggleClass('enlarged');
      }
    })
  }
  // 阅读页面标题下面加上发布时间，阅读数，评论数，点赞数
  function addPostDate() {
    var $readPostTitle = $('#post_detail .postTitle')[0];
    if ($readPostTitle) {
      var postdate = $('#post-date').text();
      var viewCount = $('#post_view_count').text();
      var commentCount = $('#post_comment_count').text();
      var diggitCount = $('#digg_count').text();

      // 有一定几率页面还没有加载出相关数据，这里延迟半秒再处理
      if (viewCount === '...' || commentCount === '...' || diggitCount === '...') {
        setTimeout(addPostDate, 500);
        return;
      }

      var star = '<svg viewBox="0 0 1040 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="29732" width="20" height="20"><path d="M520 288.192l-9.552-9.552-4-3.888c-73.28-69.088-188.432-67.488-259.808 3.888-72.848 72.848-72.848 190.96 0 263.808l262.048 262.048a16 16 0 0 0 22.624 0l262.048-262.048c72.848-72.848 72.848-190.96 0-263.808-72.848-72.848-190.96-72.848-263.808 0l-9.552 9.552z m-46.736 21.232l3.488 3.408 43.248 43.232 43.488-43.488a138.544 138.544 0 0 1 195.936 0l3.184 3.296a138.544 138.544 0 0 1-3.2 192.64L520 747.92 280.576 508.512a138.544 138.544 0 0 1 189.568-201.92l3.12 2.832z" p-id="29733" fill="#999999"></path></svg>';
      var eye = '<svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="30014" width="20" height="20"><path d="M611.232 467.904a35.92 35.92 0 0 1-35.36 29.376 35.888 35.888 0 0 1-35.936-35.84 35.888 35.888 0 0 1 35.904-35.84 105.824 105.824 0 0 0-60.96-19.2c-58.608 0-106.048 47.312-106.048 105.6s47.44 105.6 106.032 105.6 106.032-47.312 106.032-105.6c0-15.744-3.456-30.672-9.664-44.096zM516.64 720c83.456 0 157.856-35.328 217.856-94.864 43.648-43.312 74.784-98.144 74.784-113.136 0-15.504-30.432-70.16-73.536-113.28C676.288 339.264 601.728 304 516.64 304c-84.576 0-158.944 35.968-218.88 96.64C255.136 443.808 224 499.168 224 512c0 12.32 31.856 67.872 75.008 111.2C359.488 683.984 433.696 720 516.64 720z m0 48C299.744 768 176 557.536 176 512s120.064-256 340.64-256c220.592 0 340.64 204.768 340.64 256s-123.744 256-340.64 256z m-1.76-102.4c-85.088 0-154.048-68.768-154.048-153.6s68.96-153.6 154.032-153.6c85.072 0 154.032 68.768 154.032 153.6s-68.96 153.6-154.032 153.6z" p-id="30015" data-spm-anchor-id="a313x.7781069.0.i34" fill="#999999"></path></svg>';
      var comment = '<svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="30434" width="20" height="20"><path d="M519.312 392h16a16 16 0 0 1 16 16v168.352a16 16 0 0 1-16 16h-16a16 16 0 0 1-16-16V408a16 16 0 0 1 16-16zM408 436.528h16a16 16 0 0 1 16 16v79.296a16 16 0 0 1-16 16h-16a16 16 0 0 1-16-16v-79.296a16 16 0 0 1 16-16z m222.608 0h16a16 16 0 0 1 16 16v79.296a16 16 0 0 1-16 16h-16a16 16 0 0 1-16-16v-79.296a16 16 0 0 1 16-16zM418.512 719.28l0.032-0.08A311.04 311.04 0 0 0 520 736C675.2 736 800 624.32 800 488S675.2 240 520 240 240 351.68 240 488c0 70.592 33.52 136.56 91.568 183.392l-0.08 0.096 3.328 92.8 83.696-45.008z m-134-25.184C225.84 639.536 192 566.304 192 488 192 323.888 339.408 192 520 192S848 323.888 848 488 700.592 784 520 784c-32.992 0-65.328-4.4-96.224-12.944l-109.792 59.76a16 16 0 0 1-23.632-13.312l-5.84-123.408z" p-id="30435" fill="#999999"></path></svg>';
      $readPostTitle.after($('<div class="reader-post-info">' + postdate
        + '<span title="阅读数">' + eye + viewCount + '</span>'
        + '<span title="评论数">' + comment + commentCount + '</span>'
        + '<span title="点赞数">' + star + diggitCount + '</span></div>')[0]);
      
    }
  }

  loading();
  $(function () {
    // 页面加载完毕，停掉加载动画
    isLoading = false;
    // 标题下面加上发布时间
    addPostDate();
    // 载入小磁怪
    if ($('#div_digg').length === 1 || window.location.href.indexOf('\/p\/') !== -1) {
      $(document.body).append('<div id="xiaociguai"><img title="电磁波切换" alt="电磁波切换" src="https://images.cnblogs.com/cnblogs_com/vvjiang/996881/o_xiaociguai2.jpg" /></div>');

      // 绑定停止精灵球跳动按钮
      var isStopJump = false;
      var stopJump = function () {
        $('#div_digg').css('animation', 'unset');
        $('#xiaociguai').addClass('enable-electric');
        isStopJump = true;
      }
      $('#xiaociguai').click(function (e) {
        if (isStopJump) {
          $('#div_digg').css('animation', 'jumping 5s ease-in-out').css('animation-iteration-count', 'infinite');
          $('#xiaociguai').removeClass('enable-electric');
          isStopJump = false;
        } else {
          stopJump();
        }
      })
      // 精灵球被点击后直接被小磁怪吸附
      $('.diggit').click(stopJump);
      // 如果判定为曾经做过点赞或者取消点赞的操作，那么也被小磁怪吸附
      if ($('#digg_tips').text().trim() !== '') {
        stopJump();
      }
    }
    // 修改评论样式，加载评论头像
    beginModifyComment();
    // 点击头像跳转到个人信息页面
    $('#Header1_HeaderTitle').attr('href', 'https://home.cnblogs.com/u/vvjiang/');
    // 生成文章目录
    createCatalog();
    // 图片放大功能
    bindClickEvent4EnlargeImg();
  })
})();
